# 写一个打砖块小游戏。
# 编程提示：打砖块小游戏
# 游戏目标
# 玩家通过控制底部的挡板，反弹小球来击碎屏幕上方的砖块。当所有砖块被击碎时，游戏胜利；如果小球掉落到屏幕底部，游戏结束。
# 游戏功能
# 屏幕和背景：创建一个窗口显示游戏界面。
# 挡板：玩家可以左右移动挡板。
# 小球：小球会自动反弹，碰到挡板、墙壁和砖块时会改变方向。
# 砖块：屏幕上方有若干砖块，被小球击中后会消失。
# 游戏结束条件：所有砖块被击碎或小球掉落到屏幕底部。
# 得分系统：每次击碎砖块时增加分数。

import pygame
import random

# 初始化pygame
pygame.init()

# 设置屏幕大小
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))

# 设置颜色
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)

# 设置字体
font = pygame.font.Font(None, 36)

# 挡板
paddle_width = 100
paddle_height = 20
paddle = pygame.Rect(screen_width // 2 - paddle_width // 2, screen_height - 50, paddle_width, paddle_height)
paddle_speed = 7
paddle_color = red

# 小球
ball_size = 20
ball = pygame.Rect(screen_width // 2 - ball_size // 2, screen_height - 100, ball_size, ball_size)
ball_color = blue
ball_speed_x = random.choice([-5, 5])
ball_speed_y = -5

# 砖块
brick_width = 70
brick_height = 30
bricks = [pygame.Rect(x + screen_width // 10, y + screen_height // 5, brick_width, brick_height) for y in range(0, screen_height // 5, brick_height + 20) for x in range(0, screen_width - screen_width // 10, brick_width + 20)]
brick_color = green

# 得分
score = 0

# 游戏循环
running = True
while running:
    # 处理事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # 移动挡板
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT] and paddle.left > 0:
        paddle.x -= paddle_speed
    if keys[pygame.K_RIGHT] and paddle.right < screen_width:
        paddle.x += paddle_speed

    # 移动小球
    ball.x += ball_speed_x
    ball.y += ball_speed_y

    # 碰壁反弹
    if ball.left <= 0 or ball.right >= screen_width:
        ball_speed_x = -ball_speed_x
    if ball.top <= 0:
        ball_speed_y = -ball_speed_y

    # 碰挡板反弹
    if paddle.colliderect(ball):
        ball_speed_y = -ball_speed_y
        ball.bottom = paddle.top

    # 碰砖块反弹
    for brick in bricks[:]:
        if ball.colliderect(brick):
            bricks.remove(brick)
            ball_speed_y = -ball_speed_y
            score += 1
            break

    # 小球掉落到屏幕底部
    if ball.bottom >= screen_height:
        font_game_over = pygame.font.Font(None, 72)
        text_game_over = font_game_over.render("Game Over", True, white)
        screen.blit(text_game_over, (screen_width // 2 - text_game_over.get_width() // 2, screen_height // 2 - text_game_over.get_height() // 2))
        pygame.display.flip()
        pygame.time.wait(2000)
        running = False

    # 判断游戏胜利
    if len(bricks) == 0:
        font_game_win = pygame.font.Font(None, 72)
        text_game_win = font_game_win.render("You Win", True, white)
        screen.blit(text_game_win, (screen_width // 2 - text_game_win.get_width() // 2, screen_height // 2 - text_game_win.get_height() // 2))
        pygame.display.flip()
        pygame.time.wait(2000)
        running = False

    # 绘制游戏界面
    screen.fill(black)
    for brick in bricks:
        pygame.draw.rect(screen, brick_color, brick)
    pygame.draw.rect(screen, paddle_color, paddle)
    pygame.draw.ellipse(screen, ball_color, ball)
    text_score = font.render("Score: " + str(score), True, white)
    screen.blit(text_score, (10, 10))
    pygame.display.flip()

    # 控制游戏帧率
    pygame.time.Clock().tick(60)

pygame.quit()